package com.test.service.impl;

import com.test.dao.DaoSupport;
import com.test.entity.system.Menu;
import com.test.service.MenuService;
import com.test.util.PageData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Override
    public void deleteMenuById(String MENU_ID) throws Exception {
        dao.save("MenuMapper.deleteMenuById", MENU_ID);
    }

    @Override
    public PageData getMenuById(PageData pd) throws Exception {
        return (PageData) dao.findForObject("MenuMapper.getMenuById", pd);

    }

    @Override
    public PageData findMaxId(PageData pd) throws Exception {
        return (PageData) dao.findForObject("MenuMapper.findMaxId", pd);
    }

    @Override
    public List<Menu> listAllParentMenu() throws Exception {
        return (List<Menu>) dao.findForList("MenuMapper.listAllParentMenu", null);
    }

    @Override
    public void saveMenu(Menu menu) throws Exception {
        if(menu.getMENU_ID()!=null && menu.getMENU_ID() != ""){
            //dao.update("MenuMapper.updateMenu", menu);
            dao.save("MenuMapper.insertMenu", menu);
        }else{
            dao.save("MenuMapper.insertMenu", menu);
        }
    }

    @Override
    public List<Menu> listSubMenuByParentId(String parentId) throws Exception {
        return (List<Menu>) dao.findForList("MenuMapper.listSubMenuByParentId", parentId);
    }

    @Override
    public List<Menu> listAllMenu() throws Exception {
        List<Menu> rl = this.listAllParentMenu();
        for(Menu menu : rl){
            List<Menu> subList = this.listSubMenuByParentId(menu.getMENU_ID());
            menu.setSubMenu(subList);
        }
        return rl;
    }

    @Override
    public List<Menu> listAllSubMenu() throws Exception {
        return (List<Menu>) dao.findForList("MenuMapper.listAllSubMenu", null);
    }

    @Override
    public PageData edit(PageData pd) throws Exception {
        return (PageData)dao.findForObject("MenuMapper.updateMenu", pd);
    }

    @Override
    public PageData editicon(PageData pd) throws Exception {
        return (PageData)dao.findForObject("MenuMapper.editicon", pd);
    }

    @Override
    public PageData editType(PageData pd) throws Exception {
        return (PageData)dao.findForObject("MenuMapper.editType", pd);
    }
}
